package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 套餐保存
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
      //  log.info("页面传过来的套餐:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //创建分页构造器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
          //添加查询条件,根据name进行模糊查询
           wrapper.like(name!=null,Setmeal::getName,name);
           wrapper.orderByDesc(Setmeal::getUpdateTime);

         //分页查询
        setmealService.page(setmealPage,wrapper);
        //拷贝
        BeanUtils.copyProperties(setmealPage,dtoPage,"records");
          //把原来的的数据取出来
        List<Setmeal> records = setmealPage.getRecords();
           //然后进行遍历,进行操作,获得新数据
        List<SetmealDto> records1 = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
             //然后把数据拷贝进去
            BeanUtils.copyProperties(item,setmealDto);
              //根据分类id查询分类对象
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                //分类名称
                String name1 = byId.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        //把新数据放进去
        dtoPage.setRecords(records1);

        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("页面传过来的id为:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> list(@PathVariable Long id){
        log.info(String.valueOf(id));
        SetmealDto setmealDto = setmealService.listWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 停售,启售
     * @param status
     * @param ids
     * @return
     */

    @PostMapping("/status/{status}")
   // @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@PathVariable int status,Long[] ids){
        log.info("页面传过来的ids为:{}",ids);
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        List<Setmeal> setmeals1 = setmeals.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(setmeals1);
        return R.success("状态修改成功");
    }

    /**
     * 修改数据
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateWith(@RequestBody SetmealDto setmealDto){
        log.info("页面传过来的数据为:{}",setmealDto);
        setmealService.updateWith(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
